<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <title>奶羊录入</title>
    <style media="screen">
        .header {
            height: 10%;
            display: flex;
            justify-content: left;
            align-items: center;
            background: #3fd5a5;
        }

        #back {
            width: 80%;
            height: 50%;
            display: flex;
            justify-content: left;
            align-items: center;
            margin-left: 30%;
        }

        .icon {
            width: 60%;
            height: auto;
        }

        .title {
            width: 30%;
            height: auto;
            font-size: 1.5rem;
            margin: auto;
            color: #fff;
        }

        .touch-down {
            opacity: .4;
        }

        #content {
            height: 30%;
            font-size: 1.2rem;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .loading {
            height: 40%;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .loading-image {
            width: 30%;
            height: auto;
        }
    </style>
</head>

<body>
    <section class="header">
        <a class="btn-left" id="aui-btn" tapmode="touch-down">
            <section id="back">
                <img src="../../image/back-off.png" class="icon">
            </section>
        </a>
        <div class="title">奶羊录入</div>
    </section>
    <section id="content">
        RFID芯片扫描中......
    </section>
    <section class="loading">
        <img src="../../image/loading_more.gif" alt="" class="loading-image">
    </section>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/request.js"></script>
<script type="text/javascript">
    function initFront() {}

    function initEvent() {

        var iconimg = $api.byId('back');

        $api.addEvt(iconimg, 'touchend', function() {

            api.closeWin();
        });

    }

    function sound() {

        var audioStreamer = api.require('audioStreamer');

        audioStreamer.openPlayer({
            path: 'widget://sound/scanner.wav',
        }, function(ret) {

        });
    }

    function refreshFramer(){
        api.execScript({
            name: 'enterprise_dashboard',
            frameName: 'enterprise_farmer_frame',
            script: 'renderSheep();'
        });
    }

    function getLocation(serial, callback) {
        console.log(serial);
        showLocating();
        var bmLocation = api.require('bmLocation');
        bmLocation.singleLocation(function(ret, err) {
            console.log(JSON.stringify(ret));
            if (ret.status) {
                api.stopLocation();
                callback(serial, ret.location.latitude, ret.location.longitude)
            } else {
                api.toast({
                    msg: '地理位置信息获取失败',
                    duration: 2000,
                    location: 'bottom'
                });
                showScaningRFID();
            }
        });
    }

    function validation(serial, callback) {
        //替换文字
        showValidating();
        var request = new Request();
        request.sheepExists(serial, function(exists, error) {
            if (!exists) {
                callback(serial);
            } else {
                api.toast({
                    msg: '当前奶羊信息已存在!',
                    duration: 2000,
                    location: 'bottom'
                });
                showScaningRFID();
            }
        });
    }

    function enterpriseregister(serial, latitude, longitude) {
        showAdding();
        var request = new Request();
        request.enterpriseregister(serial, latitude, longitude, function(ok, error) {
            showScaningRFID();
            if (ok) {
                refreshFramer();
                api.toast({
                    msg: '录入成功!',
                    duration: 2000,
                    location: 'bottom'
                });
            } else {
                api.toast({
                    msg: '录入失败, 请重新尝试!',
                    duration: 2000,
                    location: 'bottom'
                });
            }
        });

    }

    function showScaningRFID() {
        var showScaningRFID = document.getElementById('content');
        showScaningRFID.innerHTML = "RFID芯片扫描中......";
    }

    function showValidating() {
        var showValidating = document.getElementById('content');
        showValidating.innerHTML = "校验中......";
    }

    function showLocating() {
        var showLocating = document.getElementById('content');
        showLocating.innerHTML = "获取地理位置.......";
    }

    function showAdding() {
        var showAdding = document.getElementById('content');
        showAdding.innerHTML = "增加中......";
    }

    var rfid;

    function scanRFID() {
        rfid = api.require('rfid');
        rfid.start(function(result, error) {
            console.log(result.serial);
            sound();
            validation(result.serial, function(serial) {

                getLocation(serial, function(serial, latitude, longitude) {

                    enterpriseregister(serial, latitude, longitude);
                });
            });
        });
    }

    apiready = function() {

        initEvent();

        initFront();

        scanRFID();
    };
</script>

</html>
